package com.mbyte.easy.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mbyte.easy.admin.entity.*;
import com.mbyte.easy.admin.mapper.GroupMapper;
import com.mbyte.easy.admin.service.*;
import com.mbyte.easy.common.constant.*;
import com.mbyte.easy.common.web.AjaxResult;
import com.mbyte.easy.util.OssFileUtils;
import com.mbyte.easy.util.OssUtil;
import com.mbyte.easy.util.Utility;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.time.LocalDateTime;


/**
 * @ClassName fileUpload
 * @Description: 文件上传的公共方法
 * @return 文件的路径
 * @Author: 白章荣
 * @Date : 2019/12/14/10:33
 */
@Slf4j
@Controller
@RequestMapping("admin/fileUpload")
public class fileUpload {

    @Autowired
    private IPptService pptService;
    @Autowired
    private IDraftService draftService;
    @Autowired
    private IAudioService audioService;
    @Autowired
    private IVideoService videoService;
    @Autowired
    private  IGroupService groupService;

    @Autowired
    private IAllotService allotService;
    @Autowired
    GroupMapper groupMapper;
    @Autowired
    private ApplicationEventPublisher applicationEventPublisher;


    /**
     * 文件上传
     *
     * @param file 文件
     * @return
     */
    @PostMapping
    @ResponseBody
    public AjaxResult documentMethod(@RequestParam("file") MultipartFile file) {
        if (file != null) {
            return AjaxResult.success(OssFileUtils.uploadSingleFile(file));
        }
        return AjaxResult.error();
    }


    /**
     * oss 文件下载
     *
     * @param path url 地址
     */
    @PostMapping(value = "ossDownloadFile", produces = "application/json")
    public void downloadFile(@RequestParam("path") String path, HttpServletResponse response) {

        log.info("开始文件下载===================== ");

        OssUtil.download2FileByStream(path, response);
    }

    @PostMapping(value = "VossDownloadFile", produces = "application/json")
    public void VdownloadFile(@RequestParam("pptId") String pptId, HttpServletResponse response) {
        Video video = videoService.selectById(Long.valueOf(pptId));
        log.info("开始文件下载===================== ");

        OssUtil.download2FileByStream(video.getVideoUrl(), response);
    }

    /**
     *
     * @param file ppt文件
     * @param id   稿件id
     * @return
     */
    @PostMapping("ppt")
    @ResponseBody
    public AjaxResult documentMethod(@RequestParam("file") MultipartFile file, @RequestParam("id") Long id) {
        if (file != null) {
            log.info("稿件id======================" + id);
            String url = OssFileUtils.uploadSingleFile(file);
            log.info("======url===========>>>>>>{}", url);
//                Long draftNum=Long.valueOf(draftText.trim());
//                log.info("===============稿件字数时？？？"+draftNum);
//            Draft draft = new Draft();
//            draft.setDraftTitle(draftTitle);
//            draft.setDraftText(draftText);
//            draft.setDraftNum(draftNum);
//            draft.setId(id);
//            draftService.updateById(draft);
            Ppt ppt = pptService.getOne(new QueryWrapper<Ppt>().lambda()
                    .eq(Ppt::getDraftId, id));
            if (ppt == null) {
                ppt = new Ppt();
                ppt.setCreateTime(LocalDateTime.now());
            }
            ppt.setDraftId(id);
            ppt.setPptUrl(url);
            ppt.setPptMaker(Utility.getCurrentUsername());
            ppt.setStatusCode(StatusPPTAudio.STATUS_PPT_BUTTJOJINT_DISTRIBUTION);
            ppt.setSubmitTime(LocalDateTime.now());
            ppt.setUpdateTime(LocalDateTime.now());
            ppt.setGroupId(pptService.GetGroupIdById(id).getGroupId());
            pptService.saveOrUpdate(ppt);

            //操作日志
            applicationEventPublisher.publishEvent(new DraftLog()
                    .setGroupId(draftService.getById(id).getGroupId())
                    .setActionType(DraftLogConstant.ACTION_OTHER)
                    .setCreateTime(LocalDateTime.now())
                    .setUserId(Utility.getSysUser().getId())
                    .setDraftId(id)
                    .setActionContent("上传PPT")
                    .setPrimaryState(StatusPPTAudio.STATUS_PPT_BUTTJOJINT_DISTRIBUTION));
//                    .setAfterState(StatusPPTAudio.STATUS_PPT_PPTTORECORD_UPLOADTO));

            return AjaxResult.success(url);
        }
        return AjaxResult.error();
    }

    /**
     * @author 孙祺至
     * @date 2019/12/26 10:34
     * 制作人重新上传PPT，更新状态
     */
    @PostMapping("pptReupload")
    @ResponseBody
    public AjaxResult pptReupload(@RequestParam("file") MultipartFile file, @RequestParam("id") Long id) {
        if (file != null) {
            log.info("PPTid======================" + id);
            String url = OssFileUtils.uploadSingleFile(file);
            log.info("======url===========>>>>>>{}", url);
            Ppt ppt = new Ppt();
            ppt.setId(id);
            ppt.setPptUrl(url);
            ppt.setPptMaker(Utility.getCurrentUsername());
            ppt.setStatusCode(StatusPPTAudio.STATUS_PPT_PPTTORECORD_UPLOAD);
            ppt.setSubmitTime(LocalDateTime.now());
            ppt.setCreateTime(LocalDateTime.now());
            ppt.setUpdateTime(LocalDateTime.now());
            ppt.setGroupId(pptService.selectGroupIdById(id).getGroupId());
            log.info("=====ppt=============>>>>{}", ppt);
            //更新PPT
            pptService.updateById(ppt);

            Draft draft = new Draft();
            draft.setId(id);
            draft.setStatusCode(StatusPPTAudio.STATUS_PPT_PPTTORECORD_UPLOAD);
            draftService.updateById(draft);
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    /**
     * 视频 文件下载
     *
     * @param file    文件
     * @param id      稿件的id
     * @param groupId 稿件的组的id
     * @return
     */
    @PostMapping("make")
    @ResponseBody
    public AjaxResult make(@RequestParam("file") MultipartFile file, @RequestParam("id") Long id, @RequestParam("groupId") Long groupId) {
        if (file != null) {

            log.info("组的id======================" + groupId);
            log.info("稿件的id======================" + id);
            String url = OssFileUtils.uploadSingleFile(file);


            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    /**
     * @return
     * @Author 张雪
     * @Description 上传音转视
     * @Date 2019/12/19 20:52
     * @Param file 文件
     **/
    @PostMapping("uploadAudioVideo")
    @ResponseBody
    public AjaxResult uploadAudioVideo(@RequestParam("file") MultipartFile file, @RequestParam("id") Long id, @RequestParam("groupId") String groupId) {
        if (file != null) {
            log.info("====================");
            Long currentUserId = audioService.selectIdByUsername(Utility.getCurrentUsername());
            String url = OssFileUtils.uploadSingleFile(file);
            log.info("url--> {}", url);
            Video video = videoService.getOne(new QueryWrapper<Video>().lambda()
                    .eq(Video::getDraftId, id));
            if (video == null) {
                video = new Video();
                video.setCreateTime(LocalDateTime.now());
            }
//            Allot allot = new Allot();
//            allot.setStatusCode(StatusRecordVideo.STATUS_VIDEO_AUDIOTOVIDEO_UPLOAD);//1019
            video.setDraftId(id);
            video.setVideoMaker(currentUserId);
            video.setVideoUrl(url);
            //video.setGroupId(groupId);
            video.setUpdateTime(LocalDateTime.now());
            video.setSubmitTime(LocalDateTime.now());
            video.setDraftType(StatusRecordVideo.STATUS_VIDEO_RECORD_AUVEFINALLEADER_DRAFT_TYPE1);
//            video.setStatusCode(StatusRecordVideo.STATUS_VIDEO_AUDIOTOVIDEO_UPLOAD);//1019
            video.setLastOperator(videoService.selectUserIdByUsername(Utility.getCurrentUsername()));
            video.setGroupId(groupId);
            videoService.saveOrUpdate(video);

            applicationEventPublisher.publishEvent(new DraftLog().setActionType(DraftLogConstant.ACTION_VIDEO)
                    .setGroupId(groupId)
                    .setDraftId(id));
//            Audio audio= new Audio();
//            audio.setId(id);
//            audio.setStatusCode(StatusRecordVideo.STATUS_VIDEO_AUDIOTOVIDEO_UPLOAD);
//            audioService.updateById(audio);
//            UpdateWrapper<Audio> updateWrapper = new UpdateWrapper<Audio>();
//            audio.setStatusCode(StatusRecordVideo.STATUS_VIDEO_AUDIOTOVIDEO_UPLOAD);
//            updateWrapper.eq("draft_id",Long.valueOf(draftId));

//            audioService.update(audio,updateWrapper);
            return AjaxResult.success(url);
        }
        return AjaxResult.error();
    }


    /**
     * 刘辉
     * 文件重新上传
     *
     * @param file
     * @return
     */
    @PostMapping("uploadAudioToVideo")
    @ResponseBody
    public AjaxResult uploadAudioToVideo(@RequestParam("file") MultipartFile file, @RequestParam("id") Long id) {
        if (file != null) {
            Long currentUserId = audioService.selectIdByUsername(Utility.getCurrentUsername());
            String url = OssFileUtils.uploadSingleFile(file);
            Video video = new Video();
            video.setId(id);
            video.setVideoUrl(url);
            video.setVideoMaker(currentUserId);
            video.setStatusCode(StatusRecordVideo.STATUS_VIDEO_AUDIOTOVIDEO_UPLOAD);//1019
            video.setUpdateTime(LocalDateTime.now());
            video.setCreateTime(LocalDateTime.now());
            video.setGroupId(videoService.selectAGroupIdById(id).getGroupId());
            videoService.updateById(video);

            Audio audio = new Audio();
            audio.setId(id);
            audio.setStatusCode(StatusRecordVideo.STATUS_VIDEO_AUDIOTOVIDEO_UPLOAD);//1019
            audioService.updateById(audio);
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }


    /**
     * @param draftId 稿件的id
     * @param groupId 稿件的组的id
     * @return
     * @Author 刘辉
     * @Description 上传录屏
     * @Date 2019/12/19 20:52
     * @Param file 文件
     **/
    @PostMapping("uploadPPTVideo")
    @ResponseBody
    public AjaxResult uploadPPTVideo(@RequestParam("file") MultipartFile file,
                                     @RequestParam("draftId") String draftId,
                                     @RequestParam("groupId") String groupId
    ) {
        if (file != null) {
            log.info("============draftId======" + draftId);
            log.info("===============groupId=======" + groupId);
            Long currentUserId = pptService.selectIdByUsername(Utility.getCurrentUsername());
            String url = OssFileUtils.uploadSingleFile(file);
            Video video = videoService.getOne(new QueryWrapper<Video>().lambda()
                    .eq(Video::getDraftId, draftId));
            if (video == null) {
                video = new Video();
                video.setCreateTime(LocalDateTime.now());
            }
            video.setDraftId(Long.valueOf(draftId));
            video.setVideoMaker(currentUserId);
            video.setVideoUrl(url);

            video.setGroupId(groupId);   //读取之前的分组写入到video表里
            video.setDraftType(StatusRecordVideo.STATUS_VIDEO_RECORD_AUVEFINALLEADER_DRAFT_TYPE2);
            video.setUpdateTime(LocalDateTime.now());
            video.setSubmitTime(LocalDateTime.now());
            videoService.saveOrUpdate(video);
            return AjaxResult.success(url);
        }
        return AjaxResult.error();
    }


    /**
     * 发布录屏   刘辉
     *
     * @param id
     * @param draft
     * @return
     */
    @PostMapping("addVideo")
    @ResponseBody
    public AjaxResult addVideo(Long id,
                               Draft draft,
                               Long status,
                               Long pptStatusCode,
                               String draftTitle,
                               String draftText,
                               Long draftNum,
                               String videoGroupId,
                               String fileName, String videoFilename) {
        log.info(videoFilename + "asdadsasadsaa");
        if (status != null) {
            if (status == 2028) {
                //录屏制作人做被驳回的录屏

                Draft draft1 = draftService.getById(draft.getId());
                draft1.setDraftText(draft.getDraftText());
                draft1.setDraftTitle(draft.getDraftTitle());
                draft1.setDraftNum(draft.getDraftNum());
                draftService.updateById(draft1);

                Ppt ppt = pptService.getOne(new QueryWrapper<Ppt>().lambda()
                        .eq(Ppt::getDraftId, id));
                ppt.setStatusCode(StatusRecordVideo.STATUS_RECORD_PPTTORECORD_UPLOADTO);//1027
                pptService.updateById(ppt);

                QueryWrapper<Video> queryWrapper = new QueryWrapper<>();
                queryWrapper.lambda().eq(Video::getDraftId, id);
                Video video = videoService.getOne(queryWrapper);
                if (video != null) {
                    video.setStatusCode(StatusRecordVideo.STATUS_RECORD_PPTTORECORD_UPLOADTO);//1027
                    video.setUploadFilename(videoFilename);
                    videoService.updateById(video);
                }
                applicationEventPublisher.publishEvent(new DraftLog().setActionType(DraftLogConstant.ACTION_OTHER)
                    .setDraftId(id).setActionContent("ppt录屏制作人制作被驳回录屏")
                    .setGroupId(videoGroupId)
                    .setPrimaryState(StatusRecordVideo.STATUS_RECORD_BUTTJOJINT_FAILED)
                    .setAfterState(StatusRecordVideo.STATUS_RECORD_PPTTORECORD_UPLOADTO));
            } else if (status == 2029) {
                log.info(id + "=id");
                //录屏课程对接人做被驳回的录屏
                Draft draft1 = draftService.getById(draft.getId());
                draft1.setDraftText(draft.getDraftText());
                draft1.setDraftTitle(draft.getDraftTitle());
                draft1.setDraftNum(draft.getDraftNum());
                draftService.updateById(draft1);


                Group group = groupMapper.selectByGroupId(videoGroupId);
                group.setUserId(draftService.getUserIdByUserName(Utility.getCurrentUsername()).getId());
                groupService.updateById(group);

                Ppt ppt = pptService.getOne(new QueryWrapper<Ppt>().lambda()
                        .eq(Ppt::getDraftId, id));
                ppt.setStatusCode(StatusRecordVideo.STATUS_RECORD_BUTTJOJINT_SUCCESS);//1028
                pptService.updateById(ppt);

                QueryWrapper<Video> queryWrapper = new QueryWrapper<>();
                queryWrapper.lambda().eq(Video::getDraftId, id);
                Video video = videoService.getOne(queryWrapper);
                if (video != null) {
                    video.setStatusCode(StatusRecordVideo.STATUS_RECORD_BUTTJOJINT_SUCCESS);//1028
                    video.setUploadFilename(videoFilename);
                    videoService.updateById(video);
                }
                applicationEventPublisher.publishEvent(new DraftLog().setActionType(DraftLogConstant.ACTION_OTHER)
                    .setDraftId(id)
                        .setActionContent("录屏课程对接人处理被驳回录屏")
                    .setGroupId(videoGroupId)
                    .setPrimaryState(StatusRecordVideo.STATUS_RECORD_FUNCTIONARY_FAILED)
                    .setAfterState(StatusRecordVideo.STATUS_RECORD_BUTTJOJINT_SUCCESS));
            } else if (status == 2038) {
                //课程校对人驳回录屏
                Draft draft1 = draftService.getById(draft.getId());
                draft1.setDraftText(draft.getDraftText());
                draft1.setDraftTitle(draft.getDraftTitle());
                draft1.setDraftNum(draft.getDraftNum());
                draftService.updateById(draft1);

                Ppt ppt = pptService.getOne(new QueryWrapper<Ppt>().lambda()
                        .eq(Ppt::getDraftId, id));
                ppt.setStatusCode(StatusRecordVideo.STATUS_RECORD_AUVELEADER_DISTRIBUTION);//1037
                pptService.updateById(ppt);

                QueryWrapper<Video> queryWrapper = new QueryWrapper<>();
                queryWrapper.lambda().eq(Video::getDraftId, id);
                Video video = videoService.getOne(queryWrapper);
                if (video != null) {
                    video.setStatusCode(StatusRecordVideo.STATUS_RECORD_AUVELEADER_DISTRIBUTION);//1037
                    video.setUploadFilename(videoFilename);
                    videoService.updateById(video);
                }
                 applicationEventPublisher.publishEvent(new DraftLog().setActionType(DraftLogConstant.ACTION_OTHER)
                    .setDraftId(id).setActionContent("录屏课程负责人处理被驳回录屏")
                    .setGroupId(videoGroupId)
                    .setPrimaryState(StatusRecordVideo.STATUS_RECORD_COURSEREADER_FAILED)
                    .setAfterState(StatusRecordVideo.STATUS_RECORD_AUVELEADER_DISTRIBUTION));
            } else if (status == 2025) {

                //音视频校对组长驳回录屏
                Draft draft1 = draftService.getById(draft.getId());
                draft1.setDraftText(draft.getDraftText());
                draft1.setDraftTitle(draft.getDraftTitle());
                draft1.setDraftNum(draft.getDraftNum());
                draftService.updateById(draft1);

                Ppt ppt = pptService.getOne(new QueryWrapper<Ppt>().lambda()
                        .eq(Ppt::getDraftId, id));
                ppt.setStatusCode(StatusRecordVideo.STATUS_RECORD_COURSEREADER_SUCCESS);//1038
                pptService.updateById(ppt);

                QueryWrapper<Video> queryWrapper = new QueryWrapper<>();
                queryWrapper.lambda().eq(Video::getDraftId, id);
                Video video = videoService.getOne(queryWrapper);
                if (video != null) {
                    video.setStatusCode(StatusRecordVideo.STATUS_RECORD_COURSEREADER_SUCCESS);//1038
                    video.setUploadFilename(videoFilename);
                    videoService.updateById(video);
                }
                 applicationEventPublisher.publishEvent(new DraftLog().setActionType(DraftLogConstant.ACTION_OTHER)
                    .setDraftId(id).setActionContent("课程校对人处理被驳回录屏")
                    .setGroupId(videoGroupId)
                    .setPrimaryState(StatusRecordVideo.STATUS_VIDEO_RECORD_AUVELEADER_FAILED)
                    .setAfterState(StatusRecordVideo.STATUS_RECORD_COURSEREADER_SUCCESS));
                 }
            }  if (pptStatusCode == 1036 || status == 1036) {
                //录屏制作人做被分配的录屏
                Draft draft1 = draftService.getById(draft.getId());
                draft1.setDraftText(draftText);
                draft1.setDraftTitle(draftTitle);
                draft1.setDraftNum(draftNum);
                draft1.setFileName(fileName);
                draftService.updateById(draft1);
                Long currentUserId = pptService.selectIdByUsername(Utility.getCurrentUsername());

                Allot allot = allotService.getOne(new QueryWrapper<Allot>().lambda()
                        .eq(Allot::getDraftId, id).eq(Allot::getAllotnoId,currentUserId).eq(Allot::getStatusCode,StatusRecordVideo.STATUS_RECORD_BUTTJOJINT_DISTRIBUTION),false);
                allot.setStatusCode(StatusRecordVideo.STATUS_RECORD_PPTTORECORD_UPLOADTO);//1027
                allotService.updateById(allot);

                Ppt ppt = pptService.getOne(new QueryWrapper<Ppt>().lambda()
                        .eq(Ppt::getDraftId, id),false);
                ppt.setStatusCode(StatusRecordVideo.STATUS_RECORD_PPTTORECORD_UPLOADTO);//1027
                pptService.updateById(ppt);

                QueryWrapper<Video> queryWrapper = new QueryWrapper<>();
                queryWrapper.lambda().eq(Video::getDraftId, id);
                Video video = videoService.getOne(queryWrapper);
                if (video != null) {
                    video.setStatusCode(StatusRecordVideo.STATUS_RECORD_PPTTORECORD_UPLOADTO);//1027
                    video.setLastOperator(pptService.selectIdByUsername(Utility.getCurrentUsername()));
                    video.setUploadFilename(videoFilename);
                    videoService.updateById(video);
                }
                applicationEventPublisher.publishEvent(new DraftLog().setActionType(DraftLogConstant.ACTION_OTHER)
                    .setDraftId(id).setActionContent("录屏制作人做被分配的录屏")
                    .setGroupId(videoGroupId)
                    .setPrimaryState(StatusRecordVideo.STATUS_RECORD_BUTTJOJINT_DISTRIBUTION)//1036
                    .setAfterState(StatusRecordVideo.STATUS_RECORD_PPTTORECORD_UPLOADTO));//1027
            }

        return AjaxResult.success();
    }





    /**
     * 牛英健
     * 发布视频
     */
    @PostMapping("addAudio")
    @ResponseBody
    public AjaxResult addAudio(@RequestParam("id") Long id,@RequestParam("draftText") String draftText,
                               @RequestParam("draftTitle")String draftTitle,@RequestParam("draftNum") Long draftNum,
                                @RequestParam("fileName") String fileName) {

        Audio audio= audioService.getOne(new QueryWrapper<Audio>().lambda()
                .eq(Audio::getDraftId,id));
        if(audio == null){
            return AjaxResult.error();
        }
        audio.setStatusCode(StatusRecordVideo.STATUS_VIDEO_AUDIOTOVIDEO_UPLOAD);
        audioService.updateById(audio);
        QueryWrapper<Video> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Video::getDraftId, id);
        Video video = videoService.getOne(queryWrapper);
        if (video!= null){
            video.setStatusCode(StatusRecordVideo.STATUS_VIDEO_AUDIOTOVIDEO_UPLOAD);
            videoService.updateById(video);
        }
        Draft draft = draftService.getById(id);
        draft.setStatusCode(StatusConstant.STATUS_AUDIO_SUBMIT);//1010
        draft.setDraftText(draftText);
        draft.setDraftTitle(draftTitle);
        draft.setDraftNum(draftNum);
        draft.setFileName(fileName);
        draftService.updateById(draft);
        String actionContent = "";
        if(draftService.selectRoleId() == SysRole.AUDIO_VISUAL_PRODUCER){
            actionContent = "音转视制作人发布";
        }else{
            actionContent = "稿件转视制作人发布";
        }
        applicationEventPublisher.publishEvent(new DraftLog().setActionType(DraftLogConstant.ACTION_VIDEO)
                .setDraftId(id).setGroupId(video.getGroupId())
                .setPrimaryState(StatusRecordVideo.STATUS_PPT_AUDIO_AUVEREADER_SEND)
                .setAfterState(StatusRecordVideo.STATUS_VIDEO_AUDIOTOVIDEO_UPLOAD)
                .setActionContent(actionContent));
        return AjaxResult.success();
    }
    

    /**
     * 刘雪奇
     * 音频录制人重新上传
     */
    @PostMapping("audioReupload")
    @ResponseBody
    public AjaxResult audioReupload(@RequestParam("file") MultipartFile file, @RequestParam("id") Long id) {
        log.info("id===================================================={}",id);
        if (file != null) {
            log.info("=========AudioId" + id);
            String url = OssFileUtils.uploadSingleFile(file);
            log.info("=========url",url);
            Audio audio = new Audio();
            audio.setId(id);
            audio.setAudioUrl(url);
            audio.setAudioPerson(Utility.getCurrentUsername());
            audio.setStatusCode(StatusConstant.STATUS_AUDIO_SUBMIT);//1010
            audio.setSubmitTime(LocalDateTime.now());
            audio.setCreateTime(LocalDateTime.now());
            audio.setUpdateTime(LocalDateTime.now());
            audio.setGroupId(audioService.selectAGroupIdById(id).getGroupId());

            audioService.updateById(audio);

            Draft draft = new Draft();
            draft.setId(id);
            draft.setStatusCode(StatusConstant.STATUS_AUDIO_SUBMIT);//1010
            draftService.updateById(draft);
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }


    /**
     *
     * @param file
     * @param draftId
     * @param groupId
     * @return
     */
    @PostMapping("audioMakeUnmake")
    @ResponseBody
    public AjaxResult audioMakeUnmake(@RequestParam("file") MultipartFile file,
                                       @RequestParam("draftId") String draftId,@RequestParam("groupId") String groupId) {
        if (file != null) {

        }
        return AjaxResult.error();
    }

    /**
     * 稿件导出成word
     * ,@RequestParam("id") Long id,@RequestParam("draftTitle") String draftTitle,
     *             @RequestParam("draftText") String draftText,@RequestParam("fileName") String fileName
     */
    @PostMapping("fileOut")
    public void fileOut(Draft draftHtml,HttpServletResponse response) throws IOException {
        log.info("稿件进来了吗?-->{}",draftHtml);
        Draft draft = draftService.getById(draftHtml.getId());
        String content = "<html><head><b>"+ draft.getDraftTitle()+"</b></head><body>"+draft.getDraftText()+"</body></html>";
        ByteArrayInputStream inputStream = null;
        try(
                BufferedOutputStream outputStream = new BufferedOutputStream(response.getOutputStream());){
            // 配置文件下载
            response.setHeader("content-type", "application/octet-stream");
            response.setContentType("application/octet-stream");
            // 下载文件能正常显示中文
            response.setHeader("Content-Disposition", "attachment;filename="
                    + URLEncoder.encode(StringUtils.isNoneBlank(draft.getFileName()) ?  draft.getFileName() : Utility.getRandomStrByNum(8) , "UTF-8")+".doc");
            POIFSFileSystem fs = new POIFSFileSystem();
            //对应于org.apache.poi.hdf.extractor.WordDocument
            inputStream = new ByteArrayInputStream(content.getBytes("GB2312"));
            fs.createDocument(inputStream, "WordDocument");
            fs.writeFilesystem(outputStream);
            outputStream.flush();
        } catch (Exception e) {
            log.error("下载异常！", e);
        }finally {
            if(inputStream != null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


}
